// TOWN SCRIPT
//    Town 0: Entrance

// This is the special encounter script for this town.
// The states INIT_STATE, EXIT_STATE, and START_STATE have
// meanings that are described in the documentation. States you write
// yourself should be numbered from 10-100.

// flags:
// 0, 0 = packs taken from players already.

begintownscript;

variables;

short choice, i, j, x, y, slt, itm;

body;

beginstate INIT_STATE;
	turn_off_training(1);
	set_crime_tolerance(5);

	set_flag(0, 3, 0);

	// close off house if already quit
	if (get_flag(50, 2) > 0) {
		set_terrain(22, 28, 4);
		set_terrain(23, 28, 4);
	}

	// open gate if won already
	if (get_flag(50, 1) > 0) {
		flip_terrain(21, 31);
	}
break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;
	// print_str_color("In start state.", 3);
	if (get_flag(0, 0) == 0) {
		set_flag(0, 0, 1);
		reset_dialog();
		add_dialog_str(0, "The manors caretaker shows you into a sitting room by the entrance.", 0);
		add_dialog_str(1, "The oppressive atmosphere of the house saps your energy.", 0);
		add_dialog_str(2, "'Let me take your packs.' he says as he collects them.  'They will be safe in the cloak room across the hall.' he adds.", 0);
		add_dialog_str(3, "As he somewhat flees from the room, he says over his shoulder 'Good luck.'", 0);
		add_dialog_str(4, "After stowing your packs, he slips out the front door, leaving you to your task.", 0);
		add_dialog_choice(0, "Ok");
		choice = run_dialog(1);
		// take everything now
		set_state_continue(10);
	}
break;

beginstate 10;
	// print_str_color("In state 10.", 3);
	// take all items in pack from user, leave armor, weapons, etc.
	j = 0;
	// because item_type/take_item doesn't seem to work with one pass, do it several times
	while (j < 5) {
		i = 0;
		while (i < 4) {
			if (i == 0) {
				x = 18;
				y = 30;
			}
			else if (i == 1) {
				x = 19;
				y = 30;
			}
			else if (i == 2) {
				x = 20;
				y = 30;
			}
			else {
				x = 19;
				y = 34;
			}
			if (char_ok(i) == TRUE) {
				slt = 13;
				while (slt < 40) {
					itm = item_type_in_slot(i, slt);
					if (itm != -1) {
						take_item_char_item(i, slt, x, y);
					}
					slt = slt + 1;
				}
			}
			i = i + 1;
		}
		j = j + 1;
	}
	// strip spell energy at game start
	run_scenario_script(10);
break;

beginstate 11;
	if (get_flag(0, 1) == 0) {
		set_flag(0, 1, 1);
		message_dialog("Ahead of you, across the hall, is the manors cloak room.", "You can see your pack items stored safely beyond the gate.");
	}
break;

beginstate 12;
	if (get_flag(0, 3) == 0) {
		set_flag(0, 3, 1);
		message_dialog("Ahead of you is the manor entrance.", "");
	}
break;

beginstate 13;
	if (get_flag(0, 2) == 0) {
		set_flag(0, 2, 1);
		message_dialog("The items from your packs are stacked neatly on tables within the cloak room.", "");
	}
break;

beginstate 14;
	block_entry(TRUE);
	if (char_loc_x(0) == 22) {
		x = 34;
	}
	else {
		x = 35;
	}
	move_to_new_town(1, x, 57);
break;

beginstate 15;
	block_entry(TRUE);
	reset_dialog();
	add_dialog_str(0, "Out the door lies the path to village.", 0);
	add_dialog_str(1, "Do you leave the manor (adventure elsewhere)?", 0);
	add_dialog_choice(0, "Yes");
	add_dialog_choice(1, "No");
	choice = run_dialog(1);
	if (choice == 1) {
		// leaving, end scenario now
		slt = get_flag(50, 1);
		reset_dialog();
		if (slt == 0) {
			// lost
			add_dialog_str(0, "As you walk down the path leading to the village, you realize the manor has beaten you much as it has countless others.", 0);
			add_dialog_str(1, "Maybe the next adventure won't be as difficult.", 0);
		}
		else {
			// won
			add_dialog_str(0, "As you walk down the path leading to the village, you are happy that you were able to solve the mystery and save the family.", 0);
			add_dialog_str(1, "You walk a little faster, with joy in your heart, looking forward to the next adventure.", 0);
		}
		add_dialog_choice(0, "Ok");
		choice = run_dialog(1);
		end_scenario(slt);
	}
break;
